<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\ApiBaseController;
use App\Http\Controllers\Controller;
use App\Models\CzMobileBlank;
use Illuminate\Http\Request;

class CzMobileBlankController extends ApiBaseController
{
    //
    public function getList()
    {
        $list = CzMobileBlank::orderBy('created_at', 'desc')->paginate($this->pageSize);
        return success(page_format($list));
    }
    public function add()
    {
        $mobile = request('mobile', '');
        $remark = request('remark', '');
        if (!$mobile) {
            return error('mobile 不能为空');
        }
        if (!is_array($mobile)) {
            return error('mobile 格式错误');
        }
        $success_mobile = [];
        $fail_mobile = [];
        foreach ($mobile as $key => $value) {
            $value = trim($value);
            if (strlen($value) !== 11) {
                array_push($fail_mobile, $value);
                continue;
            }
            if (CzMobileBlank::where('mobile', $value)->exists()) {
                array_push($fail_mobile, $value);
                continue;
            }
            array_push($success_mobile, $value);
            CzMobileBlank::create(['mobile' => $value, 'remark' => $remark]);
        }
        return success(['data' => [
            'success_mobile' => $success_mobile,
            'fail_mobile' => $fail_mobile,
        ]]);
    }
    public function del()
    {
        $id = request('id', '');
        if (!$id) {
            return error('id 不能为空');
        }
        if (!is_array($id)) {
            $id = [$id];
        }
        CzMobileBlank::destroy($id);
        return success();
    }
}
